package cn.wolfcode.luowowo.mgrsite.job;

import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.vo.StrategyStatisPersistenceVO;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisRedisService;
import cn.wolfcode.luowowo.cache.vo.StrategyStatisVO;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

// redis数据持久化定时任务
@Component
public class RedisDataPersistenceJob {

    @Reference
    private IStrategyStatisRedisService strategyStatisRedisService;

    @Reference
    private IStrategyDetailService strategyDetailService;

    @Scheduled(cron = "0/5 * * * * ? ")
    public void doWork(){
        System.out.println("==============vo数据持久化==start====================");
        // 1:查询所有vo对象
        List<StrategyStatisVO> voList = strategyStatisRedisService.queryStrategyStatisVoList();

        for (StrategyStatisVO vo : voList) {
            // 2:将vo对象中变动的统计数据, 持久化到mysql中
            StrategyStatisPersistenceVO strategyStatisPersistenceVO = new StrategyStatisPersistenceVO();
            BeanUtils.copyProperties(vo, strategyStatisPersistenceVO);
            strategyDetailService.updataStrategyStatis(strategyStatisPersistenceVO);
        }

        System.out.println("==============vo数据持久化==end====================");
    }
}
